<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
<!--
                <el-breadcrumb-item><i class="el-icon-menu"></i> 表格</el-breadcrumb-item>
-->
                <el-breadcrumb-item>小组列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="handle-box">
             <el-dialog :title="title" :visible.sync="dialogFormVisible">
              <el-form ref="form" :model="form" label-width="80px">
                 <el-form-item label="小组名称">
                    <el-input maxlength="10" class="handle-input" v-model="form.name"></el-input>
                </el-form-item>

              <el-form-item label="权限">
                <el-select v-model="form.authorityIds" class="handle-input" multiple placeholder="请选择..">
                 <el-option
                    v-for="item in options"
                    :key="item.authorityId"
                    :label="item.name"
                    :value="item.authorityId">
                </el-option>
           </el-select>
        </el-form-item>
                <el-form-item label="备注">
                    <el-input maxlength="100"  type="textarea" :rows="2"  placeholder="请输入内容"
                               v-model="form.description"> </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-if="isShow === true" @click="onSubmit">提交</el-button>
                    <el-button  @click="cancel">取消</el-button>
                </el-form-item>
            </el-form>
            </el-dialog>

            <el-button type="primary" icon="edit" class="handle-del mr10" @click="isShow = true, save()">新增</el-button>
        </div>
        <div class="handle-box">

            <el-input v-model="name" list.id placeholder="小组名称" class="handle-input mr10" icon="circle-close" :on-icon-click="handleIconClick"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
        </div>
        <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
           <!-- <el-table-column type="selection" width="55"></el-table-column>-->
           <el-table-column v-if="false"  prop="groupId" label="groupId"  width="150">
            </el-table-column>
            <el-table-column prop="name" label="小组名称"  style="width: 25%" >
                  <template slot-scope='scope'>
                <a style="border-bottom:solid 1px "  @click="isShow = false, Detailed(scope.$index, scope.row)"> {{scope.row.name}}</a>
                </template>
            </el-table-column>

            <el-table-column prop="description" label="备注" style="width: 25%" >
            </el-table-column>

             <el-table-column label="操作" width="170">
                <template slot-scope="scope">
                    <el-button size="small"
                            @click="isShow = true, handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                               @click="handleDelete(scope.$index, scope.row)">删除</el-button>

                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    @current-change ="handleCurrentChange"
                    @size-change="handleSizeChange"
                    layout="sizes,prev, pager, next,total"
                    :page-sizes="[10,20,50]"
                    :page-size="pageSize"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [],
                curPage: 1,
                title: '',
                pageSize: 10,
                total:0,
                title:'',
                name:'',
                options:'',
                multipleSelection: [],
                departmentName: '',
                del_list: [],
                departmentCode:'',
                category:'',
                superiorDepartmentCode:'',
                scoreType:'',
                departmentList:'',
                dialogFormVisible: false,
                authorityIds:'',
                description:'',
                name:'',
                form: {
                    groupId: null,
                    authorityIds:'',
                    name:'' ,
                    description:''
                },
                is_search: false
            }
        },
        created(){
            this.getData();
        },

        computed: {
            data(){
                const self = this;
                return self.tableData.filter(function(d){

                    let is_del = false;
                    for (let i = 0; i < self.del_list.length; i++) {

                        if(d.name === self.del_list[i].name){
                            is_del = true;
                            break;
                        }
                    }
                    return d;
                })
            },
            totalRecord(){
                return this;
            }
        },
        methods: {
            handleCurrentChange(val){
                this.curPage = val;
                this.getData();
            },
              handleSizeChange (val) {
                 this.pageSize = val;
                 this.getData();

            } ,
            getData(){
                  let self = this;
                if(process.env.NODE_ENV === 'development'){
                    self.url = '/ms/table/list';
                };

                 var searchCriteria = {};
                 searchCriteria.name = '%' + (self.name||'') + '%';

                  self.$axios.post("../adm/group/page?size="+self.pageSize+"&page="+(self.curPage-1), searchCriteria).then((res) => {
                    self.tableData = res.data;
                    self.total = res.total;

                });
            },
            handleDelete(index, row) {
                let self = this;
                self.$axios.get('../adm/group/delete?groupId='+row.groupId, {page:self.curPage}).then((res) => {
                    this.$confirm(res.message, '确定删除', {
                                  confirmButtonText: '确认',
                                  cancelButtonText: '取消',
                                  type: 'warning'
                              }).then(() => {
                                  self.$axios.get('../adm/group/delete?groupId='+row.groupId+'&force=force', {page:self.curPage}).then((res) => {
                                                      self.getData();
                                                  })
                              }).catch(error => {
                              })
                })
            },
            save() {
                let self = this;
                self.dialogFormVisible = true ;
                self.title = "添加组";
                self.form.name = '';
                self.form.authorityIds = [];
                self.form.description = '';
                self.$axios.get("../adm/authority/allAuthority", {page:self.curPage}).then((res) => {
                self.options = res.data;

                })

            },
             cancel() {
                let self = this;
                self.dialogFormVisible = false ;

            },
            onSubmit() {
                var self = this;
                self.form.departmentCode = self.departmentCode1;
                self.form.groupIds = self.groupIds;

                 if(!self.form.name){
                   this.$message.success('小组名字不可为空！');
                   return;
                }

                self.$axios({
                method: 'post',
                url: '../adm/group/save',
                handleAs:"json",
                headers:{'Content-Type':'application/json'},
                data: JSON.stringify(self.form)
               }).then(function (res) {
                     self.dialogFormVisible = false ;
                     self.$message.success('操作成功！');
                     self.getData();
             }).catch(function (err) {
             })
          },
            search(){

               let self = this;
               var searchCriteria = {};
               searchCriteria.name = '%' + (self.name||'') + '%';

               self.$axios.post("../adm/group/page?size="+self.pageSize+"&page="+(self.curPage-1), searchCriteria).then((res) => {
                   self.tableData = res.data;
                   self.total = res.total;

                });


            },
            handleIconClick(){
                let self = this;
                self.name = "";
            },
            formatter(row, column) {
                return row.name;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
             Detailed(index, row) {
                let self = this;
                self.title = '小组明细'
                self.dialogFormVisible = true;
                self.$axios.get("../adm/authority/allAuthority", {page:self.curPage}).then((res) => {
                self.options = res.data;

                })
                self.$axios.get("../adm/group/groupDetails?groupId="+row.groupId, {page:self.curPage}).then((res) => {
                      self.form = res.data;
                      self.form.authorityIds = res.data.authorityIds;
                })


            },
            handleEdit(index, row) {
                let self = this;
                self.title = "编辑小组";
                self.dialogFormVisible = true;
                self.$axios.get("../adm/authority/allAuthority", {page:self.curPage}).then((res) => {
                self.options = res.data;

                })
                self.$axios.get("../adm/group/groupDetails?groupId="+row.groupId, {page:self.curPage}).then((res) => {
                      self.form = res.data;
                      self.form.authorityIds = res.data.authorityIds;
                })

            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style scoped>
.handle-box{
    margin-bottom: 20px;
}
.handle-select{
    width: 180px;
}
.handle-input{
    width: 180px;
    display: inline-block;
}
.input{
    width: 87px;
    display: inline-block;
}
.input1{
    width: 120px;
    display: inline-block;
}
.input2{
    width: 120px;
    display: inline-block;
    height: 100px;
}
</style>
